# -*- coding:utf-8
"""
作者: Jia
日期: 2022年04月19日
描述: https://leetcode-cn.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/
"""
from typing import List


class Solution:
    def nearestValidPoint(self, x: int, y: int, points: List[List[int]]) -> int:
        idx = -1
        min_distance = float('inf')
        for i, (X, Y) in enumerate(points):
            if X == x or Y == y:
                distance = abs(X - x) + abs(Y - y)
                if distance < min_distance:
                    min_distance = distance
                    idx = i
        return idx
